package com.triplelink.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.triplelink.exception.TripleLinkException;

public final class DateTimePTUtil {

	private DateTimePTUtil() {
		
	}
	
	private static final SimpleDateFormat FORMATTER_PT = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
	public static final long MILLIS_PER_WEEK = 7L * 24L * 60L * 60L * 1000L;
	
	public static String formatToEnumerable(String dataPt) {
		
		if (StringUtil.isBlankOrNull(dataPt)) {
			
			return "";
		}
		
		return dataPt.substring(6, dataPt.length()) + dataPt.substring(3, 5) + dataPt.substring(0, 2);
	}
	
	public static Date format(String dataPt) {
		
		try {
			
			return FORMATTER_PT.parse(dataPt);
			
		} catch (ParseException e) {
			
			throw new TripleLinkException(e);
		}
	}
	
	public static String format(Date date) {
		
		return FORMATTER_PT.format(date);
	}
	
	public static long getDiffWeekdays(String dataInicioPt, String dataFimPt) {
		
		Date dataInicio =  format(dataInicioPt);
		
		Date dataFim =  format(dataFimPt);
		
		return getDiffWeekdays(dataInicio, dataFim);
	}
	
	public static long getDiffWeekdays(Date start, Date end){

	    long deltaInMillis = end.getTime() - start.getTime();
	    int deltaInWeeks = (int)(deltaInMillis / MILLIS_PER_WEEK);
	    return deltaInWeeks;
	}
}